﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;

const int N = 100007;
typedef long long LL;

struct Stone
{
	LL a, b;
	int order;
};

bool vis[N];
int cnt;
LL ans_a, ans_b;

struct cmp_a
{
	bool operator()(const Stone& s1, const Stone& s2) const {
		if (s1.a - s2.b != s2.a - s1.b)return s1.a - s2.b < s2.a - s1.b;
		return s1.a < s2.a;
	}
};

struct cmp_b
{
	bool operator()(const Stone& s1, const Stone& s2) const {
		if (s1.b - s2.a != s2.b - s1.a)return s1.b - s2.a < s2.b - s1.a;
		return s1.b < s2.b;
	}
};

priority_queue<Stone, vector<Stone>, cmp_a>qa;
priority_queue<Stone, vector<Stone>, cmp_b>qb;

void pick(int choose)
{
	if (choose) {
		while (!qa.empty() && vis[qa.top().order])qa.pop();
		if (qa.empty())return;
		Stone top = qa.top();
		qa.pop();
		vis[top.order] = true;
		ans_a += top.a;
		pick(0);
	}
	else {
		while (!qb.empty() && vis[qb.top().order])qb.pop();
		if (qb.empty())return;
		Stone top = qb.top();
		qb.pop();
		vis[top.order] = true;
		ans_b += top.b;
		pick(1);
	}
}

int main()
{
	int T;
	scanf("%d", &T);
	while (T--) {
		memset(vis, false, sizeof(vis));
		ans_a = ans_b = 0;
		scanf("%d", &cnt);
		Stone val;
		for (int i = 1; i <= cnt; i++) {
			scanf("%lld %lld", &val.a, &val.b);
			val.order = i;
			qa.push(val);
			qb.push(val);
		}
		pick(1);
		printf("%lld\n", ans_a - ans_b);
	}
	return 0;
}